﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TTBusiness.ClassDB;
using TTBusiness.DB_Linq;
using TTBusiness;
namespace TTController
{
    public class clsController
    {
        TTMarketing_DBDataContext objDB = new TTMarketing_DBDataContext();
        public List<tblCateProduct> Category_getAllCate(bool visible, bool status)
        {
            try
            {
                var l = from a in objDB.tblCateProducts
                        where a.Visible.Equals(visible) && a.Status.Equals(status)
                        select a;
                return l.ToList<tblCateProduct>();

                
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public List<tblProduct> Product_getTopProduct(int top, bool visible, bool status)
        {
            try
            {
                var l = (from a in objDB.tblProducts
                        where a.Visible.Equals(visible) && a.Status.Equals(status)
                        orderby a.ID descending
                        select a).Take(top);
                return l.ToList<tblProduct>();


            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<tblProduct> Product_getTopProductByCate(int cateID,int top, bool visible, bool status)
        {
            try
            {
                var l = (from a in objDB.tblProducts
                         where a.Visible.Equals(visible) && a.Status.Equals(status) && a.tblCate_Factory.CateID.Equals(cateID)
                         orderby a.ID descending
                         select a).Take(top);
                return l.ToList<tblProduct>();


            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblImage> Image_getTopByType(int typeid, int top)
        {
            try
            {
                var l = (from i in objDB.tblImages
                        where i.TypeID.Equals(typeid)
                        select i).Take(top);
                return l.ToList<tblImage>();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public List<tblSupport> Support_GetAll()
        {
            try
            {
                var l = from s in objDB.tblSupports
                        select s;
                List<tblSupport> list = new List<tblSupport>();
                list = l.ToList<tblSupport>();
                foreach (var item in list)
                {
                    item.Nick = item.tblTypeSupport.Pattern.Replace(item.tblTypeSupport.Parameter, item.Nick);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<tblProduct> Product_getByName(string productName, bool visible, bool status)
        {
            try
            {
                var l = (from i in objDB.tblProducts
                         where i.Name.Contains(productName) && i.Status.Equals(status) && i.Visible.Equals(visible)
                         select i);
                return l.ToList<tblProduct>();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<tblImageProduct> Image_getByProduct(int prId)
        {
            try
            {
                var l = (from i in objDB.tblImageProducts
                         where i.ProductID.Equals(prId)
                         select i);
                return l.ToList<tblImageProduct>();
            }
            catch (Exception)
            {
                throw;
            }
        }
       
    }
}
